/**
 * Created by qiushaohua on 14-3-20.
 */
define(["app",
    "angular"
], function (app, angular) {

    var deps = [];

    /**
     * 类似Bootstrap UI 里面的$$stackedMap, 不过这里是通过angular.equals 来判断key 是否相等的
     * @returns {{createNew: createNew}}
     */
    function stackedMap() {
        return {
            createNew: function () {
                var stack = [];

                return {
                    add: function (key, value) {
                        stack.push({
                            key: key,
                            value: value
                        });
                    },
                    get: function (key) {
                        for (var i = 0; i < stack.length; i++) {
                            if (angular.equals(key, stack[i].key)) {
                                return stack[i];
                            }
                        }
                    },
                    keys: function () {
                        var keys = [];
                        for (var i = 0; i < stack.length; i++) {
                            keys.push(stack[i].key);
                        }
                        return keys;
                    },
                    top: function () {
                        return stack[stack.length - 1];
                    },
                    remove: function (key) {
                        var idx = -1;
                        for (var i = 0; i < stack.length; i++) {
                            if (angular.equals(key, stack[i].key)) {
                                idx = i;
                                break;
                            }
                        }
                        return stack.splice(idx, 1)[0];
                    },
                    removeTop: function () {
                        return stack.splice(stack.length - 1, 1)[0];
                    },
                    length: function () {
                        return stack.length;
                    }
                };
            }
        };
    }

    stackedMap.$inject = deps;
    return app.lazy.service("StackedMap", stackedMap);
});